package my.interview;

import java.util.Stack;

/**
 * @DESCRIPTION:
 * @USER: shg
 * @DATE: 2022/12/29 17:54
 */
public class ReverseList {

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        ListNode two = new ListNode(2);
        ListNode three = new ListNode(3);
        head.next = two;
        two.next = three;
        three.next = null;

        printListNode(head);

        System.out.println("------");
        ListNode result = getResult(head);
        printListNode(result);

    }

    private static void printListNode(ListNode head) {
        while (head != null){
            System.out.println(head.val);
            head = head.next;
        }
    }

    public static ListNode getResult(ListNode head){
        Stack<ListNode> stack = new Stack<>();
        while (head != null){
            stack.push(head);
            head = head.next;
        }
        if(stack.isEmpty()){
            return null;
        }
        ListNode node = stack.pop();
        ListNode result = node;
        while (!stack.isEmpty()){
            ListNode temp = stack.pop();
            node.next = temp;
            node = node.next;
        }
        node.next = null;
        return result;
    }
}

class ListNode{
    int val;
    ListNode next = null;

    public ListNode(int val) {
        this.val = val;
    }
}